package com.dkm.nettysocketdemo.myWebsite.action;

import com.dkm.nettysocketdemo.myWebsite.domain.HelmetClient;
import com.dkm.nettysocketdemo.myWebsite.domain.HelmetGroup;
import com.dkm.nettysocketdemo.myWebsite.service.HelmetGroupService;
import com.dkm.nettysocketdemo.untils.Message;
import com.dkm.nettysocketdemo.untils.jwt.intertfaceceptor.domain.LocalUser;
import com.dkm.nettysocketdemo.untils.jwt.login.UserLoginToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName : HelmetGroupAction  //类名
 * @Description : 用户Action  //描述
 * @Author : zmh//作者
 * @Date: 2021-3-18 11:42  //时间
 */
@RestController
@RequestMapping("HelmetGroupAction")
public class HelmetGroupAction {

    @Autowired
    HelmetGroupService helmetGroupService;

    @Autowired
    LocalUser localUser;

    /**
     * 修改分组
     * @param helmetGroup
     * @return
     */
    @PutMapping("updateHelmetGroup")
    public Message updateHelmetGroup(HelmetGroup helmetGroup){
        Message message = new Message();
        HelmetClient client = localUser.getUser();
        int row = helmetGroupService.updateHelmetGroup(helmetGroup,client);
        message.setCode(200);
        message.setData(row);
        if (row>=1){
            message.setMsg("修改成功");
        }else{
            message.setMsg("修改失败");
        }
        return  message;
    }

    @PostMapping("insertHelmetGroup")
    @UserLoginToken
    public Message insertHelmetGroup(HelmetGroup helmetGroup){
        Message message = new Message();
        HelmetClient client = localUser.getUser();
        int row = helmetGroupService.insertHelmetGroup(helmetGroup,client);
        message.setCode(200);
        message.setData(row);
        if (row>=1){
            message.setMsg("增加成功");
        }else {
            message.setMsg("增加失败");
        }
        return  message;
    }

    /**
     * 删除分组
     * @param pkHelmetGroupId
     * @return
     */
    @DeleteMapping("deleteHelmetGroupById/{pkHelmetGroupId}")
    public Message deleteHelmetGroupById(@PathVariable("pkHelmetGroupId") String pkHelmetGroupId){
        Message message = new Message();
        int row = helmetGroupService.deleteById(pkHelmetGroupId);
        message.setCode(200);
        message.setData(row);
        if (row>=1){
            message.setMsg("删除成功");
        }else{
            message.setMsg("删除失败");
        }
        return  message;
    }

    /**
     * 查询所有分组
     * @param helmetGroup
     * @param startTime
     * @param endTime
     * @param page
     * @param limit
     * @return
     */
     @GetMapping("findAllHelmetGroup")
    public Message findAllHelmetGroup(HelmetGroup helmetGroup,String startTime,String endTime,Integer page,Integer limit){
        Message message = new Message();
        List<HelmetGroup> helmetGroups = helmetGroupService.selectAll(helmetGroup, startTime, endTime, page, limit);
        Integer rowCount = helmetGroupService.rowCount(helmetGroup, startTime, endTime);
        message.setCode(0);
        message.setCount(rowCount);
        message.setData(helmetGroups);
         return message;
    }

    /**
     * 根据客户id查询分组
     * @param clientId
     * @return
     */
    @GetMapping("findClientIdAll/{clientId}")
    public Message findClientIdAll(@PathVariable("clientId") String clientId){
        Message message = new Message();
        message.setCode(200);
        message.setMsg("查询成功");
        message.setData(helmetGroupService.findClientIdAll(clientId));
        return message;
    }

    @PutMapping("updateGroupName")
    public Message updateGroupName(String groupName,String groupId){
        Message message = new Message();
        int row = helmetGroupService.updateGroupName(groupName,groupId);
        if (row>0){
            message.setCode(200);
            message.setMsg("修改成功");
        }else {
            message.setCode(0);
            message.setMsg("修改失败");
        }
        return  message;
    }
}
